package com.xtiw.util;

public class HexString {

    public static String byteToHex(byte b){
        String hex = Integer.toHexString(b & 0xFF);
        if(hex.length()<2){
            hex = "0"+hex;
        }
        return hex;
    }

    public static  String bytesToHex(byte[] bytes){
        StringBuffer sb = new StringBuffer();
        for(byte b :bytes){
            sb.append(byteToHex(b));
        }
        return sb.toString();
    }

    public static byte hexToByte(String hex){
        return (byte)Integer.parseInt(hex,16);
    }

    public static byte[] hexToBytes(String hex){
        if(hex.length()%2==1){
            hex = "0"+hex;
        }
        hex = hex.toUpperCase();
        int length = hex.length()/2;
        byte[] bytes = new byte[length];
        for(int i=0;i<length;i++){
            bytes[i] = hexToByte(hex.substring(i*2,i*2+2));
        }
        return bytes;
    }

    public static void main(String[] args) {
        String s = "ABC";
        String a = bytesToHex(hexToBytes(s));
        System.out.println(a);
    }
}
